﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Configuration;

namespace iRemoteMulti.Forms
{
    public partial class Setup : Form
    {

        /*
         * 构造函数，初始化界面，读配置信息
         */
        private int index = 0;

        public Setup(int index)
        {
            InitializeComponent();
            this.index = index;

            readPortConfig();
            readDataSendConfig();

        }

        /*
         * 读串口配置
         */
        public void readPortConfig() 
        {
            try
            {
                comboBox1.Text = ConfigurationManager.AppSettings["PortName" + (this.index + 1)];
            }
            catch
            {
                comboBox1.Text = ConfigurationManager.AppSettings["PortName"];
            }
            try
            {
                comboBox2.Text = ConfigurationManager.AppSettings["BaudRate"+(this.index + 1)];
            }
            catch
            {
                comboBox2.Text = ConfigurationManager.AppSettings["BaudRate"];
            }
            try
            {
                comboBox3.Text = ConfigurationManager.AppSettings["DataBits" + (this.index + 1)];
            }
            catch
            {
                comboBox3.Text = ConfigurationManager.AppSettings["DataBits"];
            }
            try
            {
                comboBox4.Text = ConfigurationManager.AppSettings["StopBits" + (this.index + 1)];
            }
            catch
            {
                comboBox4.Text = ConfigurationManager.AppSettings["StopBits"];
            }
            try
            {
                comboBox5.Text = ConfigurationManager.AppSettings["Parity" + (this.index + 1)];
            }
            catch
            {
                comboBox5.Text = ConfigurationManager.AppSettings["Parity"];
            }
            try
            {
                textBox1.Text = ConfigurationManager.AppSettings["Key" + (this.index + 1)];
            }
            catch
            {
                textBox1.Text = ConfigurationManager.AppSettings["Key"];
            }
        }

        /*
         * 读重置的串口配置
         */
        public void readResetPortConfig()
        {
            comboBox1.Text = "COM" + (this.index + 1);
            comboBox2.Text = ConfigurationManager.AppSettings["BaudRate"];
            comboBox3.Text = ConfigurationManager.AppSettings["DataBits"];
            comboBox4.Text = ConfigurationManager.AppSettings["StopBits"];
            comboBox5.Text = ConfigurationManager.AppSettings["Parity"];
            textBox1.Text = ConfigurationManager.AppSettings["Key"];
        }

        /*
         * 读数据接收配置
         */
        public void readDataSendConfig()
        {
            comboBox6.Text = ConfigurationManager.AppSettings["SendInterval"];
            comboBox7.Text = ConfigurationManager.AppSettings["ReceiveTimeout"];
        }

        /*
         * 更新串口配置
         */
        public void updatePortSettings()
        {
            iRemote.channeltoport[this.index] = comboBox1.SelectedItem.ToString();
            iRemote.ports[this.index].PortName = comboBox1.SelectedItem.ToString();//串口名称
            iRemote.ports[this.index].BaudRate = Convert.ToInt32(comboBox2.SelectedItem.ToString());//波特率
            iRemote.ports[this.index].DataBits = Convert.ToInt32(comboBox3.SelectedItem.ToString());//数据位长度
            String stop_bit = comboBox4.SelectedItem.ToString();
            if (stop_bit.Equals("1"))
                iRemote.ports[this.index].StopBits = StopBits.One;
            else if (stop_bit.Equals("2"))
                iRemote.ports[this.index].StopBits = StopBits.Two;
            else
                iRemote.ports[this.index].StopBits = StopBits.None;
            String parity = comboBox5.SelectedItem.ToString();
            if (parity.Equals("Even"))
                iRemote.ports[this.index].Parity = Parity.Even;
            else if (parity.Equals("Mark"))
                iRemote.ports[this.index].Parity = Parity.Mark;
            else if (parity.Equals("Odd"))
                iRemote.ports[this.index].Parity = Parity.Odd;
            else if (parity.Equals("Space"))
                iRemote.ports[this.index].Parity = Parity.Space;
            else
                iRemote.ports[this.index].Parity = Parity.None;
            iRemote.key[this.index] = textBox1.Text;
        }

        /*
         * 保存串口配置
         */
        public void savePortConfig()
        {
            updatePortSettings();

            iRemote.saveConfig("PortName" + (this.index + 1), comboBox1.Text.ToString());
            iRemote.saveConfig("BaudRate" + (this.index + 1), comboBox2.SelectedItem.ToString());
            iRemote.saveConfig("DataBits" + (this.index + 1), comboBox3.SelectedItem.ToString());
            iRemote.saveConfig("StopBits" + (this.index + 1), comboBox4.SelectedItem.ToString());
            iRemote.saveConfig("Parity" + (this.index + 1), comboBox5.SelectedItem.ToString());
            iRemote.saveConfig("Key" + (this.index + 1), textBox1.Text);
        }

        /*
         * 保存数据接收配置
         */
        public void saveDataSendConfig()
        {
            iRemote.saveConfig("SendInterval", comboBox6.SelectedItem.ToString());
            iRemote.saveConfig("ReceiveTimeout", comboBox7.SelectedItem.ToString());
        }

        /*
         * 保存串口配置+提示
         */
        private void button1_Click(object sender, EventArgs e)
        {
            savePortConfig();
            MessageBox.Show("Save Success!");
            this.Close();
        }
        /*
         * 保存数据接收配置+提示
         */
        private void button2_Click(object sender, EventArgs e)
        {
            saveDataSendConfig();
            MessageBox.Show("Save Success!");
            this.Close();
        }
        /*
         * 重置串口配置
         */
        private void button3_Click(object sender, EventArgs e)
        {
            iRemote.resetPort();
            readResetPortConfig();
            savePortConfig();
            MessageBox.Show("Reset Success!");
            this.Close();
        }
        /*
         * 重置接收数据配置
         */
        private void button4_Click(object sender, EventArgs e)
        {
            iRemote.resetDataSend();
            readDataSendConfig();
            MessageBox.Show("Reset Success!");
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string[] portnames = SerialPort.GetPortNames();
            comboBox1.Items.Clear();//讲combobox中的端口名丢弃
            comboBox1.Text = "";
            if (portnames.Length > 0)
            {
                for (int i = 0; i < portnames.Length; i++)
                {
                    comboBox1.Items.Add(portnames[i]);//讲端口名添加到combobox中的集合中
                }
                comboBox1.Text = comboBox1.Items[0].ToString();
            }
        }
    }
}
